package com.leeh.jsontest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Liheng on 2017-04-17.
 */

public class CourseViewTwo extends LinearLayout {

    private List<TextView> textViewList;
    private ListView lvCourse;
    private Context content;
    ListViewCourseInfoAdpter adpter;
    List<ListViewCourseContent> courseContentList;

    public CourseViewTwo(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.schedule_course_type1, this, true);

        this.content = context;

        textViewList = new ArrayList<TextView>();

        textViewList.add((TextView) findViewById(R.id.title_textView));
        textViewList.add((TextView) findViewById(R.id.term_texView));
        textViewList.add((TextView) findViewById(R.id.campus_textView));
        textViewList.add((TextView) findViewById(R.id.course_number_textView));

        lvCourse = (ListView) findViewById(R.id.course_listView);

        courseContentList = new ArrayList<ListViewCourseContent>();
        adpter = new ListViewCourseInfoAdpter(content, R.layout.listview_course_content, courseContentList);
        lvCourse.setAdapter(adpter);

    }

    /**
     * 设置数据
     *
     * @param courseList
     */
    void setData(List<String> courseList) {
        for (int i = 0; i < textViewList.size() && i < courseList.size(); i++) {
            textViewList.get(i).setText(courseList.get(i));
        }
        courseContentList.clear();
        courseContentList.addAll(convertToListViewCourseContent(courseList, textViewList.size()));
        adpter.notifyDataSetChanged();
    }

    /**
     * 数据转换
     *
     * @param courseList
     * @param start
     * @return
     */
    private List<ListViewCourseContent> convertToListViewCourseContent(List<String> courseList, int start) {
        List<ListViewCourseContent> courseContentList = new ArrayList<ListViewCourseContent>();

        for (int i = start; i + 9 < courseList.size(); ) {
            courseContentList.add(new ListViewCourseContent(courseList.get(i++), courseList.get(i++),
                    courseList.get(i++), courseList.get(i++), courseList.get(i++), courseList.get(i++)
                    , courseList.get(i++), courseList.get(i++), courseList.get(i++)));
        }
        return courseContentList;
    }

}
